<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace App\CustHub\Service;

use App\CustHub\Mapper\ChSmartTagMapper;
use App\CustHub\Mapper\ChTagPresetMapper;
use Carbon\Carbon;
use FriendsOfHyperf\Lock\Exception\LockTimeoutException;
use Hyperf\Di\Annotation\Inject;
use Mine\Abstracts\AbstractService;
use Mine\Annotation\Transaction;
use function FriendsOfHyperf\Lock\lock;
use function Hyperf\Coroutine\co;
use function Hyperf\Support\optional;

/**
 * 标签预设服务类
 */
class ChTagPresetService extends AbstractService
{
    /**
     * @var ChTagPresetMapper
     */
    public $mapper;

    #[Inject]
    protected ChSmartTagMapper $smartTagMapper;

    public function __construct(ChTagPresetMapper $mapper)
    {
        $this->mapper = $mapper;
    }

    public function init(int $mainUid): void
    {
        $lock = lock('init_user_smart_tag',3);
        try {
            $lock->block(2);
            $exists = $this->smartTagMapper->model::query()
                ->where('main_uid',$mainUid)
                ->where('is_preset',1)
                ->exists();

            if(!$exists){
                $list = $this->mapper->getTagList();

                $list = $list->map(function ($item) use ($mainUid){
                    $item['uid'] = $mainUid;
                    $item['main_uid'] = $mainUid;
                    $item['module'] = $item['group_id'];
                    $item['is_preset'] = 1;
                    $item['created_at'] = Carbon::now()->toDateTimeString();
                    $item['updated_at'] = Carbon::now()->toDateTimeString();
                    unset($item['group_id']);
                    return $item;
                })->toArray();

                $tree = arrayToTree($list);

                foreach ($tree as $item){
                    unset($item['id']);
                    $parentId = $this->smartTagMapper->save($item);
                    $childrenData = [];
                    foreach ($item['children'] as $children){
                        unset($children['id']);
                        $children['parent_id'] = $parentId;
                        $childrenData[] = $children;
                    }
                    if(!empty($childrenData)){
                        $this->smartTagMapper->model::insert($childrenData);
                    }
                }
            }
        }catch (LockTimeoutException $e){
        } finally {
            optional($lock)->release();
        }
    }

    /**
     * 新增预设标签
     * @param array $data
     * @return mixed
     */
    #[Transaction]
    public function save(array $data): mixed
    {
        $parentId = $this->mapper->save($data);
        $childData = [];
        foreach ($data['tag'] as $k=>$item){
            $childData[$k]['name'] = $item;
            $childData[$k]['group_id'] = $data['group_id'];
            $childData[$k]['parent_id'] = $parentId;
            $childData[$k]['sort'] = $k;
        }
        $this->batchSave($childData);
        return $parentId;
    }
}